package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Student;
import com.han.dao.concrete.product.UserDAO;

/**
 * Servlet implementation class IndividualStudentServlet
 */
@WebServlet("/Professor/IndividualStudentServlet")
public class IndividualStudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Logger logger = Logger.getLogger(IndividualStudentServlet.class.getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndividualStudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info("Entered IndividualStudentServlet GET");
		
		HttpSession session = request.getSession();
		
		if(session.getAttribute("professor")!=null){
			
			UserDAO userdao = new UserDAO();
			String studentID = null;
			Student student = null;
			
			if(request.getParameter("id")!=null){
				studentID = request.getParameter("id"); 
			}
			else{
				
			}
			
			try {
				student = (Student)userdao.retrieveUserById(studentID);
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (NamingException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
			request.setAttribute("student", student);
			
			RequestDispatcher rd = request.getRequestDispatcher("Student_Edit.jsp");
			
			rd.forward(request, response);
			
		}
		else{
			
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
